#!/usr/bin/env Rscript

### ####################
### Package Dependencies
### ####################

library("plyr")
library("foreach")
library("emIRT")
library("pscl")
library("ggplot2")


## #########
## Set Paths
## #########

p2root <- "../"
p2estimates <- paste(p2root, "data/RDATA/estimates", sep = "")
p2Rout <- paste(p2root, "data/RDATA/analysis/", sep = "")
p2timesout <- paste(p2root, "data/CSV/times/", sep = "")

vAllFiles <- dir(p2estimates, recursive = TRUE)

## ################
## Helper Functions
## ################

reduceList1d <- function(.list) {
    thistype <- .list$output$type
                                        #
    if (thistype == "wnominate") {
        cs <- .list$output$output$fits[1]/100
        apre <- .list$output$output$fits[2]
        gmp <- .list$output$output$fits[3]
        dims <- .list$output$output$dimensions
        Nest <- sum(!is.na(.list$output$output$legislators$coord1D))
        Jest <- sum(!is.na(.list$output$output$rollcalls$midpoint1D))
    }
    if (thistype == "oc") {
        cs <- .list$output$output$fits[1]
        apre <- .list$output$output$fits[2]
        gmp <- NA
        dims <- .list$output$output$dimensions
        Nest <- sum(!is.na(.list$output$output$legislators$coord1D))
        Jest <- sum(!is.na(.list$output$output$rollcalls[, 1]))
    }
    if (thistype == "fastest" | thistype == "fastestMP" | thistype == "fastestLOW" | thistype == "fastestBS") {
        cs <- .list$output$output$fit$csr
        apre <- NA
        gmp <- .list$output$output$fit$gmp
        dims <- .list$output$output$d
        Nest <- .list$output$output$n
        Jest <- .list$output$output$j
    }
    if (thistype == "ideal") {
        cs <- .list$output$output$csr
        apre <- NA
        gmp <- NA
        dims <- .list$output$output$d
        Nest <- dim(.list$output$output$xbar)[1]
        ## Jest <- dim(.list$output$output$betabar)[1]
        Jest <- NA
    }

    hostname <- ifelse(length(.list$hostname) == 1,
                       .list$hostname,
                       ""
                       )

    return(data.frame(cham = .list$cham,
                      sess = .list$sess,
                      canchor = .list$canchor,
                      lanchor = .list$lanchor,
                      time = .list$time[3],
                      hostname = hostname,
                      mc = .list$mc,
                      type = thistype,
                      dim = dims,
                      cs = cs,
                      gmp = gmp,
                      ## N = .list$output$rc$n,
                      ## J = .list$output$rc$m,
                      Nest = Nest,
                      Jest = Jest
                      )
           )
}



## #############
## Process Times
## #############

afile <- vAllFiles[5]

## vAllFiles <- vAllFiles[4:6]

print(vAllFiles)

foreach(afile = vAllFiles
        ) %do% {
            print(afile)
            ## ############
            ## Read In File
            ## ############
            load(paste(p2estimates,
                       '/',
                       afile,
                       sep = ""
                       )
                 )
            ## ###################
            ## Reduce Output to DF
            ## ###################
            out <- ldply(.data = lOut,
                         .fun = reduceList1d
                         )

            ## ###############
            ## Write DF as CSV
            ## ###############
            tmp <- strsplit(afile, fixed = TRUE, '.')[[1]][1]
            thisfn <- paste(sub('/', '_', tmp, fixed = FALSE), ".csv", sep = "")
            write.csv(out, file = paste(p2timesout, thisfn, sep = ""))
            return(NULL)
        }


################################################################################
